package cn.com.dhcc.app.pub.core.job.cmdata;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import cn.com.dhcc.app.core.CM;
import cn.com.dhcc.app.core.base.IocBean;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.pub.core.consts.AppConst;
import cn.com.dhcc.app.pub.core.consts.AppIocBean;
import cn.com.dhcc.app.pub.core.consts.NodeInfoParam;
import cn.com.dhcc.app.pub.core.tag.AppTagEl;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.service.exch.cmdata.CmDataExchangeLifecycleImpl;
import cn.com.dhcc.turbo.service.registry.BaseInfoService;

/**
 * 数据发送定时器
 * 该定时器扫描本地待发送的数据，并将数据发送至传输管道中
 */
public class CmDataLocalSendCronJob extends QuartzJobBean {
	
	private Logger logger = Log.getLogger(LOGTYPE.QUARTZJOB);

	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
		BaseInfoService baseInfoService=(BaseInfoService) CM.getBeanByName(IocBean.BASE_INFO_SERVICEIMPL_BEAN);
		NodeInfo localNode;
		try {
			logger.info("["+AppConst.installIp+"]"+"定时任务CmDataLocalSendCronJob开始执行....");
			localNode = baseInfoService.getCurrentNodeInfo();
			 if (localNode!=null) {//节点初始化后才开始定时任务
					CmDataExchangeLifecycleImpl service = (CmDataExchangeLifecycleImpl) CM.getBeanByName(AppIocBean.CM_DATA_EXCHANGE_LIFECYCLE_BEAN);
					service.sendDataToPipline();//跨级节点发送，将数据发送至传输管道
					
					//本级节点数据的发送，直接将数据发送给目的系统
					service.sendDataToLocalSys();
					
					//内网到外网节点发送，更新状态等待刻盘
					service.waitCarvedDiscs();
					
					
				 if (AppTagEl.isInnerNet(NodeInfoParam.NODE_NET_TYPE) && baseInfoService.getCurrentNodeInfo()!=null &&AppConst.YES.equals(baseInfoService.getCurrentNodeInfo().getHasGap())) {
				    service.sendDataToInnerSendPipeline();
				 }else if(!AppTagEl.isInnerNet(NodeInfoParam.NODE_NET_TYPE) ) {
					service.waitCarvedDiscsCrossNet();
					service.sendDataToOuterSendPipeline();
				 }
					
			 }
			 logger.info("["+AppConst.installIp+"]"+"定时任务CmDataLocalSendCronJob执行结束....");
		} catch (ServiceException e) {
			logger.error("["+AppConst.installIp+"]"+"定时任务CmDataLocalSendCronJob执行时报错:"+e);
		}
		
	}
}